#coding:utf-8
from gensim.models import word2vec,Word2Vec
import logging
import pandas as pd
import codecs,json
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

# 读取文件内容,以list形式返回  [[],[],[],...[],[]]
def readFile(filename):
    fd=codecs.open(filename,"r")
    lines=fd.readlines()
    fd.close()
    return lines
if __name__ == '__main__':
    lines=readFile(u"data/rm.txt")
    words=[]
    for line in lines:
        for word in line.split():
            words.append(word)
# 切分词汇
    sentences= [s.split() for s in words]
# 构建模型
    #model = word2vec.Word2Vec(sentences, min_count=1,sg=0,hs=0)
   # model.save("data/model1")
    model=Word2Vec.load("data/model1")
# 进行相关性比较
    print model.similarity('我们','我')
    b=model.most_similar("我")
    result = json.dumps(b, encoding='UTF-8', ensure_ascii=False)
    print result
